#include "BlockTank.h"


BlockTank::BlockTank(int X, int Y)
{
	m_Sprite = Source::getInstance()->getSprite(IDImage::IMG_BlockTank);
	m_Position.x = (float)X;
	m_Position.y = (float)Y;

	m_Index = 0;
	m_Blood = 7.0f;
	m_TimeAnimation = 0.8f;
	m_Gametime = new GameTime();

	loadPositionGunAtSprite("Block Tank//PositionGunBlockTank.txt");
	m_ToolBullet = new Toolbullet(1, 2.0f);
	m_ToolBullet->setGun(IDGun::Enemy_Gun);
}
float BlockTank::GetTanX(int x)
{
	switch (x)
	{
	case 25:
		return 0.466f;
		break;
	case 50:
		return 1.192f;
		break;
	case 75:
		return 3.732f;
		break;
	case -25:
		return -0.466f;
		break;
	case -50:
		return -1.192;
		break;
	case -75:
		return -3.732f;
		break;
	}
}

void BlockTank::UpdateAnimation(float X, float Y)
{
	m_Gametime->setTime();
	if (m_Gametime->getElapsedTimePerSecond() >= m_TimeAnimation)
	{
		if (abs(m_Vpx - m_Position.x) >= SCREEN_WIDTH) return;

		float x = X - m_Position.x;
		float y = Y - m_Position.y;

		if (x == 0) x++;
		if (x >= 0)
			m_Flip = SpriteEffect::None;
		else
			m_Flip = SpriteEffect::Flip;

		if (Y >= m_Position.y)
		{
			TanX = abs(y / x);

			if (TanX > 0 && TanX <= GetTanX(25))
			{
				m_Index = DirectionBlockTank::_3_Direction;
				m_shootDirection = ShootDirection::_0_SHOOT;
			}
			else if (TanX > GetTanX(25) && TanX <= GetTanX(50))
			{
				m_Index = DirectionBlockTank::_2_Direction;
				m_shootDirection = ShootDirection::_30_SHOOT;
			}
			else if (TanX > GetTanX(50) && TanX < GetTanX(75))
			{
				m_Index = DirectionBlockTank::_1_Direction;
				m_shootDirection = ShootDirection::_60_SHOOT;
			}
			else if (TanX > GetTanX(75))
			{
				m_Index = DirectionBlockTank::_12_Direction;
				m_shootDirection = ShootDirection::_90_SHOOT;
			}
		}
		else if (Y < m_Position.y)
		{
			TanX = -abs(y / x);
			if (TanX <= 0 && TanX >= GetTanX(-25))
			{
				m_Index = DirectionBlockTank::_3_Direction;
				m_shootDirection = ShootDirection::_0_SHOOT;
			}
			else if (TanX < GetTanX(-25) && TanX >= GetTanX(-50))
			{
				m_Index = DirectionBlockTank::_4_Direction;
				m_shootDirection = ShootDirection::_330_SHOOT;
			}
			else if (TanX < GetTanX(-50) && TanX >= GetTanX(-75))
			{
				m_Index = DirectionBlockTank::_5_Direction;
				m_shootDirection = ShootDirection::_300_SHOOT;
			}
			else if (TanX < GetTanX(-75))
			{
				m_Index = DirectionBlockTank::_6_Direction;
				m_shootDirection = ShootDirection::_270_SHOOT;
			}
		}
	}
}

void BlockTank::Update()
{
	if (m_Gametime->getElapsedTimePerSecond() >= m_TimeAnimation)
	{
		m_Gametime->Update();

		for (int i = 0; i < m_positionShootAtSprite.size(); i++)
		{
			if (m_Index == m_positionShootAtSprite[i].m_Index)
			{
				m_ToolBullet->InitBullet(m_Position.x, m_Position.y, m_Sprite->getWidth(), m_Sprite->getHeight(), m_positionShootAtSprite[i].m_X, m_positionShootAtSprite[i].m_Y, m_Flip, m_shootDirection, TypeMove::Move_OnTheLand);
				m_ToolBullet->Shoot();
			}
		}
	}

	m_ToolBullet->Update();
}

void BlockTank::Draw(LPD3DXSPRITE SpriteHandler)
{
	m_Sprite->setIndex(m_Index);
	Character::Draw(SpriteHandler);
}

BlockTank::~BlockTank()
{
}
